package conson.tech.manage.scene.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.math.BigDecimal;

/**
 * @Description: TODO
 * @author: yuwenshuo
 * @since: 2020-07-02 14:06
 */

public class MoneyConverter implements Converter<Long> {

    @Override
    public Class supportJavaTypeKey() {
        return Long.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public Long convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        BigDecimal value = cellData.getNumberValue();
        return value.longValue() * 100L;
    }

    @Override
    public CellData convertToExcelData(Long aLong, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if(aLong == null){
            aLong = 0L;
        }

        String cellValue = String.valueOf(aLong / 100D);

        CellData cellData = new CellData(cellValue);
        return cellData;
    }
}
